package day03;

/**
 * 单向链表
 */
public class SingleListNode {

    private int size;
    private ListNode head;

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            this.val = x;
        }
    }

    /**
     * 新增一个节点
     * @param int val
     */
    public void addNode(int val) {
        ListNode node = new ListNode(val);
        if (size != 0) {
            node.next = head;
        }
        head = node;
        size++;
    }

    //显示节点信息
    public void display() {
        if (size > 0) {
            ListNode node = head;
            int tempSize = size;
            if (tempSize == 1) {//当前链表只有一个节点
                System.out.println("[" + node.val + "]");
                return;
            }
            while (tempSize > 0) {
                if (node.equals(head)) {
                    System.out.print("[" + node.val + "->");
                } else if (node.next == null) {
                    System.out.print(node.val + "]");
                } else {
                    System.out.print(node.val + "->");
                }
                node = node.next;
                tempSize--;
            }
            System.out.println();
        } else {//如果链表一个节点都没有，直接打印[]
            System.out.println("[]");
        }

    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public ListNode getHead() {
        return head;
    }

    public void setHead(ListNode head) {
        this.head = head;
    }
}
